package com.wang.API;

import com.wang.bean.Result;
import com.wang.bean.Singlechoice;
import com.wang.service.SinglechoiceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.List;

/**
 *
 import requests
 import hashlib
 import time

 url = "http://127.0.0.1:8080/api/functionlist"

 files = {'file':open('e:\\1.jpg','rb')}

 data = {'type':'multipart/form-data','name':'wang'}

 reponse = requests.post(url,data=data,files=files)

 text = reponse.text

 print ("text:"+text)

 */


@RestController
@RequestMapping("api")
public class SinglechoiceController {

    @Autowired
    private SinglechoiceService singlechoiceService;

    @RequestMapping("/singlechoicelistall")
    @RequiresPermissions("sys:user:singlechoicelistall")
    public Result singlechoicelistall() {
        List<Singlechoice> list = singlechoiceService.listAll();
        Result re = new Result();
        re.setCode("301");
        re.setData(list);
        return re;
    }


    @ResponseBody
    @RequestMapping(value = "/singlechoiceadd2", method = RequestMethod.POST)
    @RequiresPermissions("sys:task:singlechoiceadd2")
    public String singlechoiceadd2(HttpServletRequest request) {
        BufferedReader br;
        StringBuilder sb = null;
        String reqBody = null;
        try {


            br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line = null;
            sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            if (sb.length() < 1) return "";
            reqBody = URLDecoder.decode(sb.toString(), "UTF-8");
           // reqBody = reqBody.substring(reqBody.indexOf("{"));
            System.out.println(reqBody);
            return reqBody;

        } catch (IOException e) {
            System.out.println("获取json参数错误！{}" + e.getMessage());
            return "";
        }
    }


    /**
     * 选择题提交JSON方式
     * @param s
     * @return
     */
    @RequestMapping(value = "/singlechoiceadd")
    @RequiresPermissions("sys:task:singlechoiceadd")
    public Result singlechoiceadd(@RequestBody Singlechoice s)
    {
        Result re = new Result();
        if(s.getTitle()==null||s.getTitle().length()<2||s.getOption_a()==null||s.getOption_a().length()<1||s.getOption_b()==null||s.getOption_b().length()<1||s.getOption_c()==null||s.getOption_c().length()<1||s.getOption_d()==null||s.getOption_a().length()<1||s.getAnswer()==null||s.getAnswer().length()<1)
        {
            re.setCode("304");
            return re;
        }
        Singlechoice ss=singlechoiceService.checkSinglechoice(s);
        if(ss!=null)
        {
            re.setCode("303");
            return re;
        }
        singlechoiceService.addSinglechoice(s);
        re.setCode("302");
        return re;
    }


    /**
     * 选择题提交接口 form方式
     * @param chapter_id
     * @param lesson_id
     * @param title
     * @param image
     * @param score
     * @param difficulty
     * @param answer
     * @param option_a
     * @param option_b
     * @param option_c
     * @param option_d
     * @param option_e
     * @param option_imagea
     * @param option_imageb
     * @param option_imagec
     * @param option_imaged
     * @param option_imagee
     * @param analysis
     * @param analysis_image
     * @param remark
     * @param is_del
     * @return
     */

    @RequestMapping(value = "/singlechoiceadd6")
    @RequiresPermissions("sys:task:singlechoiceadd6")
            public Result singlechoiceadd6(
            @ApiParam(name = "chapter_id", value = "章节",required = false) @RequestParam(value = "chapter_id", required =false) String chapter_id,
            @ApiParam(name = "lesson_id", value = "课程", required = true) @RequestParam(value = "lesson_id", required =true) String lesson_id,
            @ApiParam(name = "title", value = "题目", required = true) @RequestParam(value = "title", required =false) String title,
            @ApiParam(name = "image", value = "题目图片",required = false )  @RequestParam(value = "image", required = false) String image,
            @ApiParam(name = "score", value = "来源",required = false)  @RequestParam(value = "score", required = false) String score,
            @ApiParam(name = "difficulty", value = "难度",required = false) @RequestParam(value = "difficulty", required = false) String difficulty,
            @ApiParam(name = "answer", value = "答案",required = true) @RequestParam(value = "answer", required = true) String answer,
            @ApiParam(name = "option_a", value = "选项A", required = true) @RequestParam(value = "option_a", required = false) String option_a,
            @ApiParam(name = "option_b", value = "选项B", required = true) @RequestParam(value = "option_b", required = false) String option_b,
            @ApiParam(name = "option_c", value = "选项C", required = true) @RequestParam(value = "option_c", required = false) String option_c,
            @ApiParam(name = "option_d", value = "选项D", required = true) @RequestParam(value = "option_d", required = false) String option_d,
            @ApiParam(name = "option_e", value = "选项E", required = false) @RequestParam(value = "option_e", required = false) String option_e,
            @ApiParam(name = "option_imagea", value = "选项图片") @RequestParam(value = "option_imagea", required = false) String option_imagea,
            @ApiParam(name = "option_imageb", value = "选项图片") @RequestParam(value = "option_imageb", required = false) String option_imageb,
            @ApiParam(name = "option_imagec", value = "选项图片") @RequestParam(value = "option_imagec", required = false) String option_imagec,
            @ApiParam(name = "option_imaged", value = "选项图片") @RequestParam(value = "option_imaged", required = false) String option_imaged,
            @ApiParam(name = "option_imagee", value = "选项图片") @RequestParam(value = "option_imagee", required = false) String option_imagee,
            @ApiParam(name = "analysis", value = "题目分析")@RequestParam(value = "analysis", required = false) String analysis,
            @ApiParam(name = "analysis_image", value = "题目分析图片")@RequestParam(value = "analysis_image", required = false) String analysis_image,
            @ApiParam(name = "remark", value = "备注")@RequestParam(value = "remark", required = false) String remark,
            @ApiParam(name = "is_del", value = "是否可用") @RequestParam(value = "is_del", required = false) String is_del
      )

    {
        Result re = new Result();
        Singlechoice s = new Singlechoice();
        s.setChapter_id(chapter_id);
        s.setLesson_id(lesson_id);
        s.setTitle(title);
        s.setImage(image);
        s.setScore(score);
        s.setDifficulty(difficulty);
        s.setAnswer(answer);
        s.setOption_a(option_a);
        s.setOption_b(option_b);
        s.setOption_c(option_c);
        s.setOption_d(option_d);
        s.setOption_e(option_e);

        s.setOption_imagea(("option_imagea"));
        s.setOption_imageb(("option_imageb"));
        s.setOption_imagec(("option_imagec"));
        s.setOption_imaged(("option_imaged"));
        s.setOption_imagee(("option_imagee"));


        s.setAnswer(("answer"));
        s.setAnalysis(("analysis"));
        s.setAnalysis_image(("analysis_image"));
        s.setRemark(("remark"));
        s.setIs_del(("is_del"));
        s.setSource(("source"));




        System.out.println("titile:"+s.getTitle());
        System.out.println(s.getOption_a());
        System.out.println(s.getOption_b());
        System.out.println(s.getOption_c());
        System.out.println(s.getOption_d());
        if(s.getTitle()==null||s.getTitle().length()<2||s.getOption_a()==null||s.getOption_a().length()<1||s.getOption_b()==null||s.getOption_b().length()<1||s.getOption_c()==null||s.getOption_c().length()<1||s.getOption_d()==null||s.getOption_a().length()<1||s.getAnswer()==null||s.getAnswer().length()<1)
        {
            re.setCode("304");
            return re;
        }
        Singlechoice ss=singlechoiceService.checkSinglechoice(s);
        if(ss!=null)
        {
            re.setCode("303");
            return re;
        }
        singlechoiceService.addSinglechoice(s);
        re.setCode("302");
        return re;
    }

}
